import os


# 递归读文件夹
def get_filelist(dir, Filelist):
    if os.path.isfile(dir):
        Filelist.append(dir)
        # # 若只是要返回文件文，使用这个
        # Filelist.append(os.path.basename(dir))
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            # 如果需要忽略某些文件夹，使用以下代码
            # if s == "xxx":
            # continue
            newDir = os.path.join(dir, s)
            get_filelist(newDir, Filelist)
    return Filelist


# 读文件
def read_file(file):
    f = open(file)
    lines = f.readlines()
    return lines


# 追加写文件
def write_file(lines):
    with open('new.txt', 'a+') as f:
        for line in lines:
            if line.strip() != "":
                f.write(line)


if __name__ == '__main__':
    # path = '/Users/zhangwenchao/DevEcoStudioProjects/watch/entry/src/main/java'
    # path = '/Users/zhangwenchao/AndroidStudioProjects/EggSell/app/src/main/java/com/zzcyi/eggsell'
    path = '/Users/zhangwenchao/Downloads/java'
    filelist = get_filelist(path, [])
    for file in filelist:
        lines = read_file(file)
        write_file(lines)
